﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace TMS
{
    public struct HistoryItem
    {
        public ControlsEnum ctrl;
        public DataGridViewRow parameters;
       

        // all the members of the struct has to be initialized in this way
        public HistoryItem(ControlsEnum ctrl, DataGridViewRow parameters)
        {
            this.ctrl = ctrl;
            this.parameters = parameters;
        }

    }

    public static class History
    {
        public static Stack<HistoryItem> ActionsHistoryBack = new Stack<HistoryItem>();
        public static Stack<HistoryItem> ActionsHistoryForward = new Stack<HistoryItem>();
        public static HistoryItem HI;

        public static void PushBack(HistoryItem historyItem) 
        {
            ActionsHistoryBack.Push(historyItem);
        }

        public static void PushForward(HistoryItem historyItem)
        {
            ActionsHistoryForward.Push(historyItem);
        }

        public static HistoryItem PopBack() 
        {
            if (ActionsHistoryBack.Count > 0)
            {
                PushForward(HI);
                HI = ActionsHistoryBack.Pop();
            }
                return HI;
        }

        public static HistoryItem PopForward()
        {
            if (ActionsHistoryForward.Count > 0)
            {
                PushBack(HI);
                HI = ActionsHistoryForward.Pop();
            }
            return HI;
        }

        public static void registerInHistory(ControlsEnum ctrl, DataGridViewRow parameters)
        {
            PushBack(HI);
            HI.ctrl = ctrl;
            HI.parameters = CloneWithValues(parameters);
        }    

        public static void InitHistory(ControlsEnum ctrl, DataGridViewRow parameters)
        {
            HI.ctrl = ctrl;
            HI.parameters = CloneWithValues(parameters);
        }

        public  static DataGridViewRow CloneWithValues(DataGridViewRow row)
        {
            if (row!=null)
            {
                DataGridViewRow clonedRow = (DataGridViewRow)row.Clone();
                for (Int32 index = 0; index < row.Cells.Count; index++)
                {
                    clonedRow.Cells[index].Value = row.Cells[index].Value;
                }
                return clonedRow;
            }
            else return null;
        }

    }
}
